package org.leafDomain;

import org.apache.log4j.Logger;
import org.leafDomain.combine.DataSourceToHBase;
import org.leafDomain.hbase.HBase;
import org.leafDomain.hbase.impl.HBaseImpl;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Function;

public class AppToHBase {
    private static final Logger logger = Logger.getLogger(AppToHBase.class);

    protected static <T> void start(String[] args, Function<Properties, T> func, DataSourceToHBase<T> dataSourceToHBase){
        try{
            if(Objects.isNull(args) || args.length == 0 || Objects.isNull(args[0])){
                throw new NullPointerException("配置文件路径空指针异常");
            }
            final String PATH = args[0];
            final File file = new File(PATH);
            if(!file.exists() || !file.canRead() || file.length() == 0){
                throw new IOException("配置文件不存在，不可读，或为空");
            }
            Properties config = new Properties();
            config.load(new FileReader(file));

            HBase hBase = new HBaseImpl(config);
            T dataSource = func.apply(config);

            dataSourceToHBase.setSource(dataSource);
            dataSourceToHBase.setHBase(hBase);
            dataSourceToHBase.startTransfer();
        }
        catch (Exception e){
            logger.error("配置异常", e);
        }

    }
}
